package com.wh.debeziumdemo.strategy;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 学生策略选择器
 * @author GooGoo
 * @date 2023/08/19
 */
@Component
public class StudentStrategySelector implements ApplicationContextAware {

    private Map<String, StudentStrategy> strategyHashMap = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Collection<StudentStrategy> strategies = applicationContext.getBeansOfType(StudentStrategy.class).values();
        for (StudentStrategy strategy : strategies) {
            strategyHashMap.put(strategy.getOperation(),strategy);
        }
    }
    /**
     * 选择一个策略
     * @param operation
     * @return
     */
    public StudentStrategy selectStrategy(String operation){
      StudentStrategy baseInvoiceStrategy = strategyHashMap.get(operation);
        if(baseInvoiceStrategy == null){
            throw new RuntimeException("找不到策略");
        }
        return baseInvoiceStrategy;
    }

}
